package com.bingxue.edu.management.system.service;

import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.bingxue.edu.management.system.mapper.RoleMapper;
import com.bingxue.edu.management.system.model.entity.Role;
import com.bingxue.edu.management.system.model.req.RoleReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.bingxue.edu.management.system.model.entity.table.RoleTableDef.ROLE;

/**
 * 角色业务实现
 *
 * @author panbo
 * @since 2024/07/17 15:41
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleService extends BaseService<RoleMapper, Role> {

    /**
     * 创建角色
     * @param req 请求
     */
    public void createRole(RoleReq req) {
        ValidationUtils.throwIf(isRoleNameExists(req.getName()), "角色名称已存在");
        ValidationUtils.throwIf(isRoleCodeExists(req.getCode()), "角色编码已存在");

        Role newRole = this.copy(req);
        this.save(newRole);
    }

    public boolean isRoleNameExists(String name) {
        return this.queryChain().where(ROLE.NAME.eq(name)).exists();
    }

    public boolean isRoleCodeExists(String code) {
        return this.queryChain().where(ROLE.CODE.eq(code)).exists();
    }
}
